﻿using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.AutoMapperExtend;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories.Internal;
using Core.Toolkit.AutoMapperExtend.DataDictionary;
using Core.Toolkit.Common;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Core.SimpleTemp.Application.ServiceApp.SysApp.DataDicApp
{

    [AutoDi(typeof(IDataDicHelp))]
    public class DataDicHelp : IDataDicHelp
    {
        private readonly IDataDicPool _dataDicPool;

        public DataDicHelp(IDataDicPool dataDicPool)
        {
            _dataDicPool = dataDicPool;
        }

        public List<KeyValue<string, List<KeyValue<string, string>>>> GeDataDicValueByCategory(params string[] categorys)
        {
            List<KeyValue<string, List<KeyValue<string, string>>>> ret = new List<KeyValue<string, List<KeyValue<string, string>>>>();
            foreach (var item in categorys)
            {
                var list = this.GeDataDicValueByCategory(item);
                ret.Add(new KeyValue<string, List<KeyValue<string, string>>>() { Key = item, Value = list });
            }
            return ret;
        }

        public List<KeyValue<string, string>> GeDataDicValueByCategory(string category)
        {
            //先去字典池查
            var ret = _dataDicPool.GeDataDicValueByCategory(category);
            if (ret == null)
                //未找到后在去全区字典池查
                ret = ConcurrentDataDicPool.GeDataDicValueByCategory(category);
            if (ret == null)
                throw new CustomOperationException($"未找到 category:{category}对应数据，请确认category是否正确");
            return ret;
        }

        public string GetDicValue(string category, string key)
        {
            //先去字典池查
            var ret = _dataDicPool.GetDicValue(category, key);
            if (ret == null)
                //未找到后在去全区字典池查
                ret = ConcurrentDataDicPool.GetDicValue(category, key);
            if (ret == null)
                throw new CustomOperationException($"未找到 category:{category}对应数据,key:{key}，请确认category,key是否正确");
            return ret;
        }
    }
}
